package com.qhad.ads.activity;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Browser;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.songheng.eastnews.apk.R;
import com.qhad.ads.sdk.interfaces.IQhLandingPageListener;

import java.net.URISyntaxException;


public class CustomLandingPageActivity extends Activity {

    public static String ldUrl;
    public static IQhLandingPageListener listener;
    private final String MSG = "CustomLandPageActivity";
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_landing_page);
        if (ldUrl == null) {
            finish();
            return;
        }
        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setDatabaseEnabled(true);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            webView.getSettings().setDatabasePath(webView.getContext().getFilesDir().getAbsolutePath() + "/databases/");
        }
        webView.getSettings().setBuiltInZoomControls(false);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                try {
                    //调用SDK下载文件功能，需回调onAppDownload
                    if (listener.onAppDownload(url))
                        return true;
                } catch (Exception e) {
                    Log.e(MSG, "unable process onAppDownload!");
                }
                if (url.startsWith("http://") || url.startsWith("https://")) {
                    return false;
                }

                Intent intent;
                // Perform generic parsing of the URI to turn it into an Intent.
                try {
                    intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                } catch (URISyntaxException ex) {
                    Log.e(MSG, "DeepLink Bad URI!");
                    return false;
                }
                // Sanitize the Intent, ensuring web pages can not bypass browser
                // security (only access to BROWSABLE activities).
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.setComponent(null);
                // Pass the package name as application ID so that the intent from the
                // same application can be opened in the same tab.
                intent.putExtra(Browser.EXTRA_APPLICATION_ID,
                        view.getContext().getPackageName());
                try {
                    view.getContext().startActivity(intent);
                } catch (ActivityNotFoundException ex) {
                    Log.e(MSG, "No application can handle!");
                    return false;
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                listener.onPageLoadFinished();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                listener.onPageLoadFailed();
            }
        });
        webView.loadUrl(ldUrl);
    }

    @Override
    protected void onDestroy() {
        webView.loadUrl("about:blank");
        webView.destroy();
        webView = null;
        if (listener != null)
            listener.onPageClose();
        listener = null;
        super.onDestroy();
    }
}
